//
//  XYAnalysis.m
//  小小通讯录
//
//  Created by qianfeng on 15/8/12.
//  Copyright (c) 2015年 qianfeng. All rights reserved.
//

#import "XYAnalysis.h"
#import "XYContactPerson.h"


@implementation XYAnalysis{
    NSMutableDictionary * _contactdic;
}
- (id)init{
    if (self = [super init]) {
        _contactdic = [[NSMutableDictionary alloc] init];
    }
    return self;
}
- (void) analysisWithPath:(NSString *)path{
    NSString * fileStr = [[NSString alloc] initWithContentsOfFile:path  encoding:NSUTF8StringEncoding error:nil];
    
    NSMutableArray * marray = (NSMutableArray *)[fileStr componentsSeparatedByString:@"\n"];
//    NSLog(@"%@",marray);
    
    for (int i = 0 ; i < marray.count; i++) {
        
        NSString * tstr = [marray objectAtIndex:i];
        
        if (tstr.length == 0) {
            [marray removeObjectAtIndex:i]; //删除当前空串
            i--;
        }
    }
    
    for (int i = 0; i < marray.count; i++) {
        NSString * contactStr = [marray objectAtIndex:i];
       
        NSArray * array = [contactStr componentsSeparatedByString:@","];
        
        XYContactPerson * person = [[XYContactPerson alloc] initWithArray:array];
        
        [_contactdic setObject:person forKey:person.name];
    }
    
    for (NSString * key in _contactdic) {
        XYContactPerson * person = [_contactdic objectForKey:key];
        NSLog(@"%@",person);
    }
 
}

- (void)showAllInformation{
    for (NSString * key in _contactdic) {
        XYContactPerson * person = [_contactdic objectForKey:key];
        NSLog(@"%@",person);
    }
}


- (void)findWithName:(NSString *)name{
    NSArray * keys = [_contactdic allKeys];
    for (NSString * key in keys) {
        if ([key isEqualToString:name]) {
            XYContactPerson * person = [_contactdic objectForKey:name];
            NSLog(@"%@",person);
            return;
        }
    }
    NSLog(@"该联系人不存在!");
}

-(void)addWithPerson:(XYContactPerson *)person{
    [_contactdic setObject:person forKey:person.name];
    printf("添加联系人成功\n");
}

- (void)deleteWithName:(NSString *)name{
    NSArray * keys = [_contactdic allKeys];
    for (NSString * key in keys) {
        if ([key isEqualToString:name]) {
            [_contactdic removeObjectForKey:key];
            printf("删除成功!");
            return;
        }
    }
    NSLog(@"此联系人不存在!");
}

- (XYContactPerson *)alterWithName:(NSString *)name{
    NSArray * keys = [_contactdic allKeys];
    for (NSString * key in keys) {
        if ([key isEqualToString:name]) {
            XYContactPerson * person = [_contactdic objectForKey:name];
            NSLog(@"%@",person);
            return person;
        }
    }
    NSLog(@"该联系人不存在!");
    return nil;
}
@end







